require(["jquery",'public','weui'],function($,public,weui) {
	var status = public.getUrlParam('status') || 0;
	var txt = status == 1 ? '出售中的产品' : (status == 0 ? '仓库中的产品' : (status == 2 ? '已出售的产品' : '产品列表'));
    public.headertitle(txt,MODULE+'PublicJump/productAdd');
    public.footer(1);
    public.allocator();

    function getData(){
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: {status: status, type: 2,token: public.getSession('token')},
            url: MODULE+'Goods/home_goods',           
        }).success(function (res) {
            console.log(res);  
            if(res.code == 3){
                $.alert(res.msg,function(){
                    window.history.go(-1);
                });
                return false;
            } 
            var _html = '';
            if(!res.data || !res.data.length){
            	$('.products_main').addClass('products_none');
            	return false;
            }
            res.data.forEach(function(i){
                _html += '<dl class="products_item">'
					+		'<dt class="products_item_shopInfo">'
					+		'</dt>'
					+		'<dd>'
					+			'<a class="products_item_productInfo_link" href="'+MODULE+'PublicJump/productInfo/id/'+i.goods_id+'">'
					+				'<div class="products_item_productInfo">'
					+					'<div class="img_box">'
					+						'<img src="'+PUBLIC+i.goods_picture_path+'" />'
					+					'</div>'
					+					'<div class="text_box">'
					+						'<p>'+i.goods_name+'</p>'
					+						'<span>￥'+i.goods_price+'</span>'
					+					'</div>'
					+				'</div>'
					+			'</a>'
					+			'<div class="products_item_btnset" data-id="'+i.goods_id+'">'
					+				'<a class="bx4 edit '+(status==2?'disabled-':'')+'" href="'+MODULE+'PublicJump/productEdit/id/'+i.goods_id+'">编辑</a>'
					+				'<a class="bx4 delete '+(status==2?'disabled-':'')+'" href="javascript:void(0);">删除</a>'
					+				'<a class="bx4 '+(status==0?'ok':'off')+' '+(status==2?'disabled-':'')+'" href="javascript:void(0);">'+(status==0?'上架':'下架')+'</a>'
					+			'</div>'
					+		'</dd>'
					+	'</dl>'; 
            });
            $('.products_main').append(_html);
        }).fail(function (jqXHR, textStatus, errorThrown) {});
    }

    getData();

    //页面加载完毕
    public.loaded();

    //上下架
    $('.products_main').on('click','.off,.ok',function(event){
    	var $this = $(this);
    	if($this.hasClass('on')){
    		return false;
    	}
    	if($this.hasClass('disabled')){
    		$.alert('已出售的产品不可下架');
    		return false;
    	}
    	var id = $this.closest('.products_item_btnset').data('id');
    	$this.addClass('on');
    	$.ajax({
            type: 'post',
            dataType: 'json',
            data: {id: id, status: status,token: public.getSession('token')},
            url: MODULE+'Goods/up_and_down',           
        }).success(function (res) {
        	if(res.code == 0){
            	$.alert((status == 0) ? '上架成功' : '下架成功',function(){
            		window.location.href = MODULE+'PublicJump/myProduct/status/'+status+'/match/'+(Math.random()*100 >> 1);
            	}); 
            }else{
            	$.alert('操作失败');
            }
            $this.removeClass('on');
        }).fail(function (jqXHR, textStatus, errorThrown) {});
    });

    //删除
    $('.products_main').on('click','.delete',function(event){
    	var $this = $(this);
    	if($this.hasClass('disabled')){
    		$.alert('已出售的产品不可删除');
    		return false;
    	}
    	var id = $this.closest('.products_item_btnset').data('id');
    	$.ajax({
            type: 'post',
            dataType: 'json',
            data: {id: id,token: public.getSession('token')},
            url: MODULE+'Goods/delete_goods',           
        }).success(function (res) {
        	if(res.code == 0){
            	$.alert('删除成功',function(){
            		$this.closest('.products_item').remove();
            	});            	
            }else{
            	$.alert('删除失败');
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {});
    });

})